﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;
using SBACN.WebSite.Cache;

namespace SBACN.WebSite.Admin
{
    public partial class ActivityMediaEdit : BasePage
    { /// <summary>
        /// 是否是扩展编辑
        /// </summary>
        public bool ExEdit
        {
            get
            {
                if (ViewState["EditType"] == null)
                {
                    return true;
                }
                else
                {
                    return Convert.ToBoolean(ViewState["EditType"]);
                }
            }
            private set
            {
                ViewState["EditType"] = value;
            }
        }

        /// <summary>
        /// 是否发布到英文站点
        /// </summary>
        public bool IsEN
        {
            get
            {
                if (ViewState["IsEN"] == null)
                {
                    return false;
                }
                else
                {
                    return Convert.ToBoolean(ViewState["IsEN"]);
                }
            }
            private set
            {
                ViewState["IsEN"] = value;
            }
        }

        /// <summary>
        /// 查看新闻的系统字典类型
        /// </summary>
        public string SystemType
        {
            get
            {
				//if (ViewState["SystemType"] == null)
				//    return "00020004";//要闻导读，默认值
				//else
				//    return ViewState["SystemType"].ToString();
				return this.SystemCode;
            }
            private set
            {
				ViewState["SystemType"] = this.SystemCode;
            }
        }


        public int NewsID
        {
            get
            {
                if (ViewState["NewsID"] == null)
                {
                    return -1;
                }
                else
                {
                    return Convert.ToInt32(ViewState["NewsID"]);
                }
            }
            private set
            {
                ViewState["NewsID"] = value;
            }
        }

        public int NewsID_a
        {
            get
            {
                if (ViewState["NewsID_a"] == null)
                {
                    return -1;
                }
                else
                {
                    return Convert.ToInt32(ViewState["NewsID_a"]);
                }
            }
            private set
            {
                ViewState["NewsID_a"] = value;
            }
        }

        /// <summary>
        /// 是否为新增
        /// </summary>
        public bool IsAdd
        {
            get
            {
                if (ViewState["IsAdd"] == null)
                {
                    return false;
                }
                else
                {
                    return Convert.ToBoolean(ViewState["IsAdd"]);
                }
            }
            private set
            {
                ViewState["IsAdd"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                if (!AdminBasePage.CheckCodeRight(AdminBasePage.AdminInfo, SystemCode))
                {
                    AdminBasePage.RedirectWithMessage(AdminDefaultPage, "您没有访问当前页面的权限,请与系统管理员联系!");
                    return;
                }
                if (!string.IsNullOrWhiteSpace(Request["st"]))
                {
                    this.SystemType = Request["st"].Trim();
                }
                if (!string.IsNullOrWhiteSpace(Request["ad"]))
                {
                    this.IsAdd = Request["ad"].Trim() == "1";
                }
                else
                {
                    this.IsAdd = false;
                }

                int newsID = 0;
                if (!string.IsNullOrWhiteSpace(Request["id"]) && int.TryParse(Request["id"], out newsID))
                {
                    this.NewsID = newsID;
                }
                int newsID_a = 0;
                if (!string.IsNullOrWhiteSpace(Request["aid"]) && int.TryParse(Request["aid"], out newsID_a))
                {
                    this.NewsID_a = newsID_a;
                }
                InitCtrl();
            }
        }

        private void InitCtrl()
        {
            TB_SystemMap map = SystemMapCache.Instance[this.SystemType];
            this.Header.Title = "内容编辑:" + map.Name;
            if (NewsID > 0)//edit
            {
                TB_MediaReports news = DAL.DALHelper.Instance.GetObject<TB_MediaReports>("ID", NewsID);
                RenderPage(news);
            }
        }

        private void RenderPage(TB_MediaReports news)
        {
            if (news != null)
            {
                this.txt_MName.Text = news.MediaName;
                this.txt_ReportDate.Text = news.ReportDate.ToString("yyyy-MM-dd");
                this.txt_title.Text = news.Title.ToString();
                this.txt_Content.Text = news.Description;
                this.NewsID = news.ID;
            }
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txt_title.Text))
            {
                this.lbl_Message.Text = "标题不能为空";
                return;
            }
            if (string.IsNullOrWhiteSpace(txt_ReportDate.Text))
            {
                this.lbl_Message.Text = "发布时间不能为空";
                return;
            }
            DateTime dtCreate = DateTime.Now;
            if (!DateTime.TryParse(this.txt_ReportDate.Text, out dtCreate))
            {
                this.lbl_Message.Text = "发布时间格式不正确";
                return;
            }

            if (NewsID > 0)//Edit
            {
                TB_MediaReports news = DAL.DALHelper.Instance.GetObject<TB_MediaReports>("ID", NewsID);
                if (news != null)
                {
                    news.Title = this.txt_title.Text.Trim();
                    news.MediaName = this.txt_MName.Text.ToString();
                    news.Description = this.txt_Content.Text;
                    news.ReportDate = dtCreate;
                    news.CreateDate = DateTime.Now;
                    news.CreateBy = AdminBasePage.AdminInfo.UserName;
                }
                DAL.DALHelper.Instance.UpdateWithTransaction<TB_MediaReports>(news, "ID");
            }
            else//Create
            {
                TB_MediaReports news = DAL.DALHelper.Instance.Create<TB_MediaReports>();
                news.ActivityID = this.NewsID_a;
                news.Title = this.txt_title.Text.Trim();
                news.MediaName = this.txt_MName.Text.ToString();
                news.Description = this.txt_Content.Text;
                news.ReportDate = dtCreate;
                news.CreateDate = DateTime.Now;
                news.CreateBy = AdminBasePage.AdminInfo.UserName;
                DAL.DALHelper.Instance.Add<TB_MediaReports>(news);
            }
            DAL.DALHelper.Instance.SaveChanges();
			RefreshNewsList();
        }

		private void RefreshNewsList()
		{
			string clientJs = "<script type=\"text/javascript\" language=\"javascript\">";
			clientJs += "if(window.opener && window.opener.RefreshNewsList) window.opener.RefreshNewsList();";
			clientJs += "window.close();</script>";
			Response.Write(clientJs);
			Response.End();
		}
    }
}